<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    div {
      width: 200px;
      height: 200px;
      background-color: #38f;
    }
  </style>
  <body>
    <div class="box" id="box" name="box"></div>
    <script>
      //   通过元素id获取节点;
      var box = document.getElementById("box");
      box.onclick = function () {
        box.style.backgroundColor = "pink";
      };

      //   通过元素name属性获取节点;
      var box = document.getElementsByName("box");
      box[0].onclick = function () {
        box[0].style.backgroundColor = "pink";
      };

      //   通过元素标签获取节点;
      var box = document.getElementsByTagName("div");
      box[0].onclick = function () {
        box[0].style.backgroundColor = "pink";
      };

      var box = document.querySelector(".box");
      box.onclick = function () {
        box.style.backgroundColor = "pink";
      };

      var box = document.querySelectorAll(".box");
      box[0].onclick = function () {
        box[0].style.backgroundColor = "pink";
      };
    </script>
  </body>
</html>
